package com.pang.security.auth;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @version 1.0
 * 功能目的描述 ：用于  封装输出Json格式的类       ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-23 15:30
 */

public abstract class JSONAuthentication {

    protected void WriteJSON(HttpServletRequest request,
                             HttpServletResponse response,
                             Object data) throws IOException {
        // important   错误的话页面获取不到正常的 Json数据集
        response.setContentType("application/json;charset=UTF-8");
        //允许跨域请求
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Method","POST,GET");

        //输出Json
        PrintWriter out = response.getWriter();
        out.write(new ObjectMapper().writeValueAsString(data));
        out.flush();
        out.close();

    }
}
